<template>
	<view class="page">
		
		<view>
			<image v-for="item in jieshaotou"  mode="widthFix"  style="width: 100%;" :src="item['ad_pic']" @click="bannerClick(item)"></image>
		</view>
		<view class="home-header">
		  <view class="swipe-banner">
		    <u-swiper :list="bannerList" name='ad_pic' height="380" bg-color="transparent" @click="bannerClick(item)" border-radius="18"></u-swiper>
		  </view>
		</view>
		<view>
			<image v-for="item in jieshaoList"  mode="widthFix" style="width: 100%;" :src="item['ad_pic']" @click="bannerClick(item)"></image>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				myuserinfo: {},
				wxUserInfo: {},
				myHeadStyle: {},
				cosUrl: '',
				bgheadSrc: '',
				isInviteModelShow: false,
				inviterInfo: {},
				wecode: '',
				avatarUrl: '',
				gender: '',
				nickName: '游客',
				inviterID: '',
				bannerList:[],
				jieshaoList:[],
				jieshaotou:[],
				isLogin: 0, //判断是否登陆
			}
		},
		onShow() {
			if (uni.getStorageSync('token')) {
				this.isLogin = 1
			} else {
				this.isLogin = 0
			}
		},
		onLoad(option) {
			if(option.shareUserId){
				this.$store.commit('setshareUserId', option.shareUserId)
			}
			// #ifdef MP-WEIXIN

			uni.login({
				provider: "weixin",
				success: loginRes => {
					this.wecode = loginRes.code
				},
				fail: (err) => {
					this.$toast(JSON.stringify(err));
				},
			});
			// #endif
			this.getBannerList();
		},
		onShareTimeline(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: '曜宇生活',
				path: '/pages/tabBar/Home?shareUserId='+this.$store.state.userId
			}
		},
		methods: {
			getBannerList() {
				// 企业介绍
				const data3 = {
					tag: 'mobile_live_banner'
				}
				this.$http.getAdByTag(data3).then(res => {
					if(res.status == 200){
						this.jieshaotou = res.data
					}
				})
				// 企业介绍轮播
				const data = {
					tag: 'mobile_group_banner'
				}
				this.$http.getAdByTag(data).then(res => {
					if(res.status == 200){
						this.bannerList = res.data
					}
				})
				// 企业介绍
				const data2 = {
					tag: 'pc_Index_banner'
				}
				this.$http.getAdByTag(data2).then(res => {
					if(res.status == 200){
						this.jieshaoList = res.data
					}
				})
				
			},
			bannerClick(item){
				let ad = item;
				this.$u.route({url:ad.ad_url,type:ad.ad_url_type})
				return;
				if (this.isLogin == 1) {
					this.$u.route({url:ad.ad_url,type:ad.ad_url_type})
				} else {
					// #ifndef MP-WEIXIN
					this.$navigateTo('/pagesB/login/login')
					// #endif
					// #ifdef MP-WEIXIN
					// this.$navigateTo('/platforms/mp-weixin/miniLogin')
					this.wechatLogin()
					// #endif
				}
			},
			// #ifdef MP-WEIXIN
			// 微信小程序登录   code换区 小程序openid 和 session_key
			getOpenID() {
				return new Promise((resolve) => {
					this.$http.getWechatMiniProgramOpenid({
						code: this.wecode
					}).then(res => {
						resolve(res.data)
			
					});
				})
			},
			
			async wechatLogin() {
				this.inviterID = this.inviterInfo && this.inviterInfo.id
				const {openid='',session_key='',unionid=''} = await this.getOpenID()
				uni.setStorageSync('xcxOpenid', openid); //存取小程序openid
				uni.setStorageSync('xcxSessionKey', session_key);
				this.asyncUserInfo(openid, unionid);
				this.isInviteModelShow = false;
			
			},
			asyncUserInfo(openid = '', unionid = '') {
				if (openid == '') {
					return false;
				}
				this.$http.thirdLogin({
					oauth: 'weixin_mp',
					openid: openid,
					unionid: unionid,
					nick_name: this.nickName,
					uniongender: this.gender,
					iconurl: this.avatarUrl,
					inviter: this.inviterID
				}).then(result => {
					this.$toast(result.status)
					if (result.status == 200) {
						this.$store.commit('loginStatus', true)
						uni.setStorageSync('token', result.data.token);
						uni.setStorageSync('identity', result.data.identity);
						uni.setStorageSync('role', result.data.role);
						uni.setStorageSync('role_id', result.data.shop_id);
						uni.closeSocket({
							success() {
								console.log('socket关闭成功');
							},
							fail(res) {
								console.log(res);
							}
						})
						setTimeout(() => {
							this.im();
							this.$u.route({
								url: "/pages/tabBar/my",
								type: "reLaunch"
							})
						}, 500);
			
					}
					if (result.status == 400 || result.data.info == 'unregister') {
						console.log(result)
						this.$u.route({
							url: 'platforms/mp-weixin/miniLogin',
							params: {
								oauth: 'weixin_mp',
								openid: openid,
								unionid: unionid,
								nick_name: this.nickName,
								uniongender: this.gender,
								iconurl: this.avatarUrl,
								inviter: this.inviterID,
							}
						});
					}
				})
			},
			// #endif
		}
	}
</script>

<style>
page,.page {
		background: #fff;
		overflow: auto;
		height: 100%;
	}
</style>
